//
//  MainDetailHeadCell.m
//  YaDiuDiu
//
//  Created by hejinhui on 2017/10/27.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "MainDetailHeadCell.h"
#import "SDCycleScrollView.h"
#import "MainDetailModel.h"
#import "NSString+Extension.h"
@interface MainDetailHeadCell()<SDCycleScrollViewDelegate>
/*轮播图*/
@property(nonatomic,strong)UIView *headerView;
/*描述*/
@property(nonatomic,strong)UILabel *subLabel;
/*现在的价格*/
@property(nonatomic,strong)UILabel *nPriceLabel;
/*以前的价格*/
@property(nonatomic,strong)UILabel *oldPriceLabel;
/*进度条*/
@property(nonatomic,strong)UIProgressView *sliderView;
@property(nonatomic,strong)UILabel *sliderLabel;

/*计时器view*/
@property(nonatomic,strong)UIView *timeView;
/*轮播图*/
@property(nonatomic,strong)SDCycleScrollView *scyleView;



@end

@implementation MainDetailHeadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
+(instancetype)tableViewCellWithTableView:(UITableView *)tableView
{
    
    static NSString *cellId = @"cellId";
    MainDetailHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        
        cell  =[[MainDetailHeadCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        //轮播图
        CGFloat currentWidth = ScreenWidth;
        CGFloat currentHeight = currentWidth * 341 / 375;
        
        self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, currentHeight)];
        self.headerView.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:self.headerView];
        
        //scycle轮播图
        self.scyleView = [SDCycleScrollView cycleScrollViewWithFrame:self.headerView.bounds delegate:self placeholderImage:[UIImage imageNamed:@""]];
        [self.headerView addSubview:self.scyleView];
        
        //描述
        self.subLabel = [[UILabel alloc]init];
        self.subLabel.numberOfLines = 0;
        self.subLabel.font = [UIFont systemFontOfSize:16];
        self.subLabel.textColor = COLOR_RGB(38, 38, 38, 1);
        [self.contentView addSubview:self.subLabel];
        
        //现在的价格
        self.nPriceLabel = [[UILabel alloc]init];
        self.nPriceLabel.textColor = COLOR_RGB(253, 17, 66, 1);
        self.nPriceLabel.font = [UIFont systemFontOfSize:17];
        [self.contentView addSubview:self.nPriceLabel];
        
        //以前的价格
        self.oldPriceLabel = [[UILabel alloc]init];
        self.oldPriceLabel.textColor = COLOR_RGB(128, 128, 128, 1);
        self.oldPriceLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.oldPriceLabel];
        
        //进度view
        self.sliderView = [[UIProgressView alloc]init];
        [self.contentView addSubview:self.sliderView];
        //进度label
        self.sliderLabel = [[UILabel alloc]init];
        self.sliderLabel.font = [UIFont systemFontOfSize:10];
        self.sliderLabel.textColor = COLOR_RGB(128,128, 128, 1);
        [self.contentView addSubview:self.sliderLabel];
        
        //计时器
        
//        self.timeView = [[[NSBundle mainBundle]loadNibNamed:@"TimerView" owner:self options:nil] lastObject];
//        [self.contentView addSubview:self.timeView];
//        self.timeView.hidden = YES;

        
    }
    return self;
}

#pragma mark -- scyleVIewDelegate
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    
}

-(void)setModel:(MainDetailModel *)model
{
    _model = model;
    
    //轮播图
    NSMutableArray *arr = [NSMutableArray array];
    for (NSDictionary *op in model.img) {
        NSString *str = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[op objectForKey:@"img"]];
        [arr addObject:str];
    }
    self.scyleView.imageURLStringsGroup = arr;
 
    //描述
    self.subLabel.text = [NSString stringWithFormat:@"%@",model.subject];
    self.subLabel.frame = (CGRect){{13,CGRectGetMaxY(self.scyleView.frame)+10},[self.subLabel.text sizeWithFont:[UIFont systemFontOfSize:16] maxW:(ScreenWidth - 2*13)]};
    
    //现价
    self.nPriceLabel.text = [NSString stringWithFormat:@"¥ %@",model.pprice];
    self.nPriceLabel.frame = (CGRect){{13, CGRectGetMaxY(self.subLabel.frame)+10},{100,18}};
    
    //以前的价格
    self.oldPriceLabel.text = [NSString stringWithFormat:@"¥ %@",model.oprice];
    //中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:self.oldPriceLabel.text attributes:attribtDic];
    self.oldPriceLabel.attributedText = attribtStr;
    
    self.oldPriceLabel.frame = (CGRect){{13, CGRectGetMaxY(self.nPriceLabel.frame)+5},{100,[model.pprice sizeWithFont:[UIFont systemFontOfSize:15]].height}};
    
    
    self.sliderLabel.text = [NSString stringWithFormat:@"已抢购%@%@",model.Proportion,@"%"];
    //进度条数量
    [self.sliderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-10);
        make.centerY.equalTo(self.nPriceLabel);
        make.width.mas_offset([self.sliderLabel.text sizeWithFont:[UIFont systemFontOfSize:10]].width+3);
        make.height.mas_offset(15);
        
    }];
    //进度条
    self.sliderView.progressTintColor = [UIColor redColor];
    self.sliderView.progress = [model.Proportion floatValue]/60;
    [self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        
     make.right.equalTo(self.sliderLabel.left).offset(-5);
        make.centerY.equalTo(self.nPriceLabel);
        make.width.mas_offset(60);
        make.height.mas_offset(5);
        
    }];
    
    //计时器view
//    [self.timeView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.centerY.equalTo(self.oldPriceLabel);
//        make.width.mas_equalTo(100);
//        make.leading.equalTo(self.sliderView.leading);
//    }];
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
